package com.doit.day01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.protobuf.generated.TableProtos;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

/**
 * Hbase的java客户端连接hbase的时候，只需要连接zookeeper的集群
 * 就可以找到你Hbase集群的位置
 * 核心的对象：
 * Configuration：HbaseConfiguration.create();
 * Connection:ConnectionFactory.createConnection(conf);
 * Admin：conn.getAdmin();操作Hbase系统DDL，对名称空间等进行操作
 */
public class CreateTableDemo {
    public static void main(String[] args) throws Exception {
        //获取到hbase的配置文件对象
        Configuration conf = HBaseConfiguration.create();
        //针对配置文件设置zk的集群地址
        conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181");
        //创建hbase的连接对象
        Connection conn = ConnectionFactory.createConnection(conf);

        Admin admin = conn.getAdmin();

        //获取到操作hbase操作表的对象
        TableDescriptorBuilder java = TableDescriptorBuilder.newBuilder(TableName.valueOf("java"));

        //表添加列族需要集合的方式
        ArrayList<ColumnFamilyDescriptor> list = new ArrayList<>();
        //构建一个列族的构造器
        ColumnFamilyDescriptorBuilder col1 = ColumnFamilyDescriptorBuilder.newBuilder("f1".getBytes(StandardCharsets.UTF_8));
        ColumnFamilyDescriptorBuilder col2 = ColumnFamilyDescriptorBuilder.newBuilder("f2".getBytes(StandardCharsets.UTF_8));
        ColumnFamilyDescriptorBuilder col3 = ColumnFamilyDescriptorBuilder.newBuilder("f3".getBytes(StandardCharsets.UTF_8));
        //构建列族
        ColumnFamilyDescriptor build1 = col1.build();
        ColumnFamilyDescriptor build2 = col2.build();
        ColumnFamilyDescriptor build3 = col3.build();
        //将列族添加到集合中去
        list.add(build1);
        list.add(build2);
        list.add(build3);

        //给表设置列族
        java.setColumnFamilies(list);
        //构建表的描述器
        TableDescriptor build = java.build();
        //创建表
        admin.createTable(build);

        conn.close();
    }
}
